package model.pieces;

import java.util.Observable;

import model.mediator.HubColleague;
import model.mediator.HubMediator;
import model.memento.MementoSupport;
import model.memento.Movement;

public class Piece extends Observable implements HubColleague {

	/**
	 * row <-> y
	 */
	protected int row;

	/**
	 * col <-> x
	 */
	protected int col;

	/**
	 * Messages Hub
	 */
	protected HubMediator mediator;

	/**
	 * Memento support para los movimientos de la pieza.
	 */
	protected MementoSupport mementoSupport;

	/**
	 * @param col
	 * @param row
	 */
	public Piece(int col, int row) {
		this.row = row;
		this.col = col;
	}

	/**
	 * @param col
	 * @param row
	 */
	public void setPosition(int col, int row) {
		this.row = row;
		this.col = col;

		// notificar el movimiento
		this.setChanged();
		this.notifyObservers(this);
	}

	/**
	 * @param move
	 */
	public void setPosition(Movement move) {
		this.row = move.getRow();
		this.col = move.getCol();
		
		// notificar el movimiento
		this.setChanged();
		this.notifyObservers(this);
	}

	/**
	 * @return the row
	 */
	public int getRow() {
		return row;
	}

	/**
	 * @return the col
	 */
	public int getCol() {
		return col;
	}

	/**
	 * @param mediator
	 *            the mediator to set
	 */
	public void setMediator(HubMediator mediator) {
		this.mediator = mediator;
		this.mediator.addBroadcastColleague(this);
	}

	/**
	 * @param memento
	 *            the memento to set
	 */
	public void setMementoSupport(MementoSupport mementoSupport) {
		this.mementoSupport = mementoSupport;
	}

	public void suspendMementoNotifications(boolean suspend) {
		this.mementoSupport.setNotificationSuspended(suspend);
	}
	
	/**
	 * Comportamiento por defecto de Wall y Target.
	 * 
	 * @return boolean pieza puede moverse
	 */
	public boolean canMove() {
		return false;
	}

	/**
	 * Movimiento multiple requerido.
	 * Comportamiento por defecto de Wall y Man.
	 */
	@Override
	public void requestMultiMovement(Movement move) {
		if (move.isMovementPosition(col, row)) {
			this.mediator.refuseMovement(this, move);
		}
	}

	/**
	 * Movimiento simple requerido.
	 * Comportamiento por defecto de Wall, Man y Box.
	 */
	@Override
	public void requestSingleMovement(Movement move) {
		if (move.isMovementPosition(col, row)) {
			this.mediator.refuseMovement(this, move);
		}
	}

	/**
	 * Movimiento requerido denegado.
	 * Comportamiento por defecto de Wall.
	 */
	@Override
	public void refuseMovement(Movement move) {
	}

	/**
	 * Posicion (col, row) abandonada.
	 * Comportamiento por defecto de Man, Box y Wall.
	 */
	@Override
	public void leavePosition(int col, int row) {
	}

}
